package com.puff.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.puff.constant.MessageConstant;
import com.puff.constant.StatusConstant;
import com.puff.dto.SetmealDTO;
import com.puff.dto.SetmealPageQueryDTO;
import com.puff.entity.Dish;
import com.puff.entity.Setmeal;
import com.puff.entity.SetmealDish;
import com.puff.exception.DeletionNotAllowedException;
import com.puff.exception.SetmealEnableFailedException;
import com.puff.mapper.DishMapper;
import com.puff.mapper.SetmealDishMapper;
import com.puff.mapper.SetmealMapper;
import com.puff.result.PageResult;
import com.puff.service.SetmealService;
import com.puff.vo.DishItemVO;
import com.puff.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 套餐业务实现
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDTO
     */
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        // 将套餐数据添加到套餐表setmeal中
        setmealMapper.insert(setmeal);

        // 获取新增的套餐id
        Long setmealId = setmeal.getId();

        // 获取新增的套餐中的所有菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        // 将新添加的套餐菜品信息添加到setmeal_dish表中
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 起售中的套餐不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)){
                // 如果是起售状态的商品，则不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        // 删除setmeal套餐表中的数据
        setmealMapper.deleteByIds(ids);
        // 删除setmeal_dish套餐菜品表中的数据
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 根据id查询套餐，用与修改页面回显数据
     * @param id
     * @return
     */
    public SetmealVO getByIdWithDishes(Long id) {
        // 首先根据id查询setmeal数据
        Setmeal setmeal = setmealMapper.getById(id);

        // 在根据setmealId到setmeal_dish表中查询菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        // 在封装到setmealVO中，并返回给前端
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        // 1.修改套餐表
        setmealMapper.update(setmeal);

        // 2.删除套餐和菜品原来的关联关系，操作setmeal_dish表，执行delete
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        // 3.重新插入套餐和菜品新的的关联关系，操作setmeal_dish表，执行insert
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });

        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 起售停售套餐
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        // 起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == StatusConstant.ENABLE){
            /* select a.* from dish a
               left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?  */
            List<Dish> dishList = dishMapper.getBySetmealId(id);

            if (dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
